// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReportsManager.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="ReportsManager" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Linq;

    using Hisaab.Business.Entities;
    using Hisaab.Business.Interfaces;
    using Hisaab.Data.Interfaces;

    using OrderByType = Hisaab.Data.Entities.OrderByType;
    using SortByType = Hisaab.Data.Entities.SortByType;
    using Transaction = Hisaab.Data.Entities.Transaction;

    /// <summary>
    /// Defines the <seealso cref="ReportsManager"/> type.
    /// </summary>
    public class ReportsManager : IReportsManager
    {
        /// <summary>
        /// Member variable _transactionManager
        /// </summary>
        private readonly ITransactionRepository _transactionRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="ReportsManager"/> class.
        /// </summary>
        /// <param name="transactionRepository">The transaction manager.</param>
        public ReportsManager(ITransactionRepository transactionRepository)
        {
            Contract.Requires<ArgumentNullException>(transactionRepository != null);

            _transactionRepository = transactionRepository;
        }

        #region Implementation of IReportsManager

        /// <summary>
        /// Gets the global summary.
        /// </summary>
        /// <returns>Report object</returns>
        public SummaryReport GetGlobalSummary()
        {
            var report = new SummaryReport();
            var startDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            var endDate = DateTime.Today;
            var queryBetweenDates = _transactionRepository.QueryBetweenDates(new Transaction(), startDate, endDate, SortByType.Date, OrderByType.Ascending, null);
            var expanse = queryBetweenDates.Where(x => x.Amount < 0 && x.RelatedTransactionId.HasValue && x.RelatedTransactionId == 0).Sum(x => x.Amount);
            report.Expanse = Math.Abs(expanse);
            var income = queryBetweenDates.Where(x => x.Amount >= 0 && x.RelatedTransactionId.HasValue && x.RelatedTransactionId == 0).Sum(x => x.Amount);
            report.Income = income;
            report.ExpectedSaving = report.Income - expanse;
            var transactions = _transactionRepository.Query(new Transaction()).ToList();
            report.TotalWorth = transactions.Sum(x => x.Amount);

            var startDate2 = DateTime.Today.AddMonths(-1).AddDays(-DateTime.Today.Day - 1);
            var endDate2 = DateTime.Today.AddMonths(-1);
            var queryLastMonth = _transactionRepository.QueryBetweenDates(new Transaction(), startDate2, endDate2, SortByType.Date, OrderByType.Ascending, null);
            var expanse2 = queryLastMonth.Where(x => x.Amount < 0 && x.RelatedTransactionId.HasValue && x.RelatedTransactionId == 0).Sum(x => x.Amount);
            var income2 = queryLastMonth.Where(x => x.Amount >= 0 && x.RelatedTransactionId.HasValue && x.RelatedTransactionId == 0).Sum(x => x.Amount);

            report.PercentExpanseChange = expanse2 == 0 ? 0 : ((expanse - expanse2) / expanse2) * 100;
            report.PercentIncomeChange = income2 == 0 ? 0 : ((income - income2) / income2) * 100;

            return report;
        }

        #endregion Implementation of IReportsManager
    }
}